package com.example.q_kang.hhh.home;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.q_kang.hhh.R;
import com.hjq.toast.ToastUtils;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.shizhefei.view.coolrefreshview.SimpleOnPullListener;
import com.sskj.base.MVPFragment;
import com.sskj.base.adapter.slimadapter.IViewInjector;
import com.sskj.base.api.Constant;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.bean.HomeCommonBean;
import com.sskj.base.bean.OrderCommmonBean;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.ImageUtil;
import com.sskj.base.utils.SPUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * @author Hey
 * Create at  2019/04/09
 */
public class HomeFragment extends MVPFragment<HomePresenter> {

    @BindView(R.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @BindView(R.id.iv_header)
    ImageView ivHeader;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_integral_machine)
    TextView tvIntegralMachine;
    @BindView(R.id.ll_integral_machine)
    LinearLayout llIntegralMachine;
    @BindView(R.id.tv_integral_activate)
    TextView tvIntegralActivate;
    @BindView(R.id.ll_integral_activate)
    LinearLayout llIntegralActivate;
    @BindView(R.id.tv_integral_credit)
    TextView tvIntegralCredit;
    @BindView(R.id.ll_integral_credit)
    LinearLayout llIntegralCredit;
    @BindView(R.id.tv_activate_code)
    TextView tvActivateCode;
    @BindView(R.id.ll_activate_code)
    LinearLayout llActivateCode;
    @BindView(R.id.tv_paidan_coin)
    TextView tvPaidanCoin;
    @BindView(R.id.ll_paidan_coin)
    LinearLayout llPaidanCoin;
    @BindView(R.id.tv_gam_fenhong)
    TextView tvGamFenhong;
    @BindView(R.id.ll_gam_fenhong)
    LinearLayout llGamFenhong;
    @BindView(R.id.tv_gam_profit)
    TextView tvGamProfit;
    @BindView(R.id.ll_gam_profit)
    LinearLayout llGamProfit;
    @BindView(R.id.tv_gam_zhongjie)
    TextView tvGamZhongjie;
    @BindView(R.id.ll_gam_zhongjie)
    LinearLayout llGamZhongjie;
    @BindView(R.id.tv_gam_cost)
    TextView tvGamCost;
    @BindView(R.id.ll_gam_cost)
    LinearLayout llGamCost;
    @BindView(R.id.llc_activate_code)
    LinearLayout llcActivateCode;
    @BindView(R.id.llc_paidan_coin)
    LinearLayout llcPaidanCoin;
    @BindView(R.id.llc_manager_invite)
    LinearLayout llcManagerInvite;
    @BindView(R.id.btn_sell_gem)
    Button btnSellGem;
    @BindView(R.id.btn_buy_gem)
    Button btnBuyGem;
    @BindView(R.id.iv_level)
    ImageView ivLevel;
    @BindView(R.id.tv_user_level)
    TextView tvUserLevel;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_home;
    }

    @Override
    public HomePresenter getPresenter() {
        return new HomePresenter();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            if (!TextUtils.isEmpty((String) SPUtils.get(App.INSTANCE, SpConfig.Head, ""))) {
                ImageUtil.setCircleImage(App.INSTANCE,
                        SPUtils.get(App.INSTANCE, SpConfig.Head, ""),
                        ivHeader);
            }
            mPresenter.getHomeDate(null);

        }
    }

    @Override
    public void initView() {
        mPresenter.getHomeDate(null);

        coolRefreshView.addOnPullListener(new SimpleOnPullListener() {
            @Override
            public void onRefreshing(CoolRefreshView refreshView) {
                mPresenter.getHomeDate(coolRefreshView);
            }
        });
        ClickUtil.click(300, llcManagerInvite, view -> InviteManagerActivity.start(getActivity()));
        ClickUtil.click(300, llcActivateCode, view -> TeamJiHuoActivity.start(getActivity()));
        ClickUtil.click(300, llcPaidanCoin, view -> CoinPindanSendActivity.start(getActivity()));
        ClickUtil.click(300, llIntegralMachine, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment3), "5"));
        ClickUtil.click(300, llIntegralActivate, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment5), "6"));
        ClickUtil.click(300, llIntegralCredit, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment7), "10"));
        ClickUtil.click(300, llActivateCode, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment9), "3"));
        ClickUtil.click(300, llPaidanCoin, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment11), "4"));
        ClickUtil.click(300, llGamFenhong, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment13), "11"));
        ClickUtil.click(300, llGamProfit, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment15), "8"));
        ClickUtil.click(300, llGamZhongjie, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment17), "7"));
        ClickUtil.click(300, llGamCost, view -> MacIntegralDetailActivity.start(getActivity(), App.INSTANCE.getString(R.string.appJavaHomeFragment19), "9"));
        ClickUtil.click(300, btnBuyGem, view -> {
//            ToastUtils.show(App.INSTANCE.getString(R.string.not_open));
            mPresenter.getOrderCommonData();
//            if (SPUtils.get(App.INSTANCE, SpConfig.BANK, "").equals("1")
//                    || SPUtils.get(App.INSTANCE, SpConfig.ALIPAY, "").equals("1")
//                    || SPUtils.get(App.INSTANCE, SpConfig.WECHAT, "").equals("1")) {
//            } else {
//                ToastUtils.show(App.INSTANCE.getString(R.string.nopayway));
//            }
        });
        ClickUtil.click(300, btnSellGem, view -> {
//            ToastUtils.show(App.INSTANCE.getString(R.string.not_open));
            SellGamActivity.start(getActivity());
//            if (SPUtils.get(App.INSTANCE, SpConfig.BANK, "").equals("1")
//                    || SPUtils.get(App.INSTANCE, SpConfig.ALIPAY, "").equals("1")
//                    || SPUtils.get(App.INSTANCE, SpConfig.WECHAT, "").equals("1")) {
//            } else {
//                ToastUtils.show(App.INSTANCE.getString(R.string.nopayway));
//            }
        });
    }

    @Override
    public void initData() {
        tvName.setText(App.INSTANCE.getString(R.string.appJavaHomeFragment21) + SPUtils.get(App.INSTANCE, SpConfig.MOBILE, ""));
    }


    public static HomeFragment newInstance() {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        fragment.setArguments(bundle);
        return fragment;
    }

    public void getHomeDateSuccess(HomeCommonBean data) {
        if (data == null) {
            return;
        }
        if (data.getUserMoney() != null) {
            tvIntegralMachine.setText(data.getUserMoney().getFive());
            tvIntegralActivate.setText(data.getUserMoney().getSix());
            tvIntegralCredit.setText(data.getUserMoney().getTen());
            tvActivateCode.setText(data.getUserMoney().getThree());
            tvPaidanCoin.setText(data.getUserMoney().getFour());
            tvGamFenhong.setText(data.getUserMoney().getEleven());
            tvGamProfit.setText(data.getUserMoney().getEight());
            tvGamZhongjie.setText(data.getUserMoney().getSeven());
            tvGamCost.setText(data.getUserMoney().getNine());
        }
        if (data.getLevelInfo() != null) {
            tvUserLevel.setText(data.getLevelInfo().getName_cn());
        }
        if (data.getPd_level().equals("V0")) {
            ivLevel.setImageResource(R.mipmap.icon_v0);
        } else if (data.getPd_level().equals("V1")) {
            ivLevel.setImageResource(R.mipmap.icon_v1);
        } else if (data.getPd_level().equals("V2")) {
            ivLevel.setImageResource(R.mipmap.icon_v2);
        } else if (data.getPd_level().equals("V3")) {
            ivLevel.setImageResource(R.mipmap.icon_v3);
        } else if (data.getPd_level().equals("V4")) {
            ivLevel.setImageResource(R.mipmap.icon_v4);
        }
        if (data.getUserinfo() != null) {
            ImageUtil.setCircleImage(App.INSTANCE, data.getUserinfo().getHead_img(), ivHeader);
        }
    }

    public void getOrderDataSucess(OrderCommmonBean data) {
        if (data == null) {
            ToastUtils.show(App.INSTANCE.getString(R.string.fail_user_msg));
        }
        if (Integer.valueOf(data.getIs_place_order()) > 0) {
            ToastUtils.show(App.INSTANCE.getString(R.string.can_not_deal));
            return;
        }
        BuyGamActivity.start(getActivity());
    }
}
